<?php

class ScoreController extends BaseController {

    public function index() {

        $iCurrentCourse = CNamHoa::CurrentCourse();

        return View::make("score.page", [
                'sPage_Title'       => trans('navigation.mornitor.score'),
                'sPage_Description' => trans('navigation.mornitor-description.score'),
                'iCurrentCourse'    => $iCurrentCourse,
                'aClasses'          => Classes::where('course', $iCurrentCourse)->get(),
            ]);
    }

    public function postAjaxLoadUser() {

        $Classes = Classes::find(Input::get('class_id'));
        $aUser   = $Classes ? $Classes->user()->thieunhi()->get() : [ ];
        $aUserCore = [ ];

        $Option = Cache::get('options');

        foreach ($Option['DOT_KIEM_TRA'] as $key => $Object) {
            if ($Object->status == 'UNPUBLISH') {
                unset($Option['DOT_KIEM_TRA'][$key]);
            }
        }
        foreach ($Option['LAN_KIEM_TRA'] as $key => $Object) {
            if ($Object->status == 'UNPUBLISH') {
                unset($Option['LAN_KIEM_TRA'][$key]);
            }
        }

        if ($aUser->count()) {
            foreach (UserScore::hocluc($aUser->lists('id'))->get() as $Info) {

                $aUserCore[$Info->user_id][$Info->scoretype][$Info->data] = $Info;
            }
        }

        return View::make("score.table", [
                'aUser'     => $aUser,
                'Option'    => $Option,
                'aUserCore' => $aUserCore,
            ]);
    }

    public function postAjaxLoadForm() {
        $User   = User::find(Input::get('user_id'));
        $Option = Cache::get('options');

        $Score     = UserScore::hocluc(Input::get('user_id'), Input::get('DOT_KIEM_TRA'), Input::get('LAN_KIEM_TRA'))->first();
        $aScoreLog = UserScoreLog::hocluc(Input::get('user_id'), Input::get('DOT_KIEM_TRA'), Input::get('LAN_KIEM_TRA'))->get();

        return View::make("score.modal", [
                'User'         => $User,
                'DOT_KIEM_TRA' => Input::get('DOT_KIEM_TRA'),
                'LAN_KIEM_TRA' => Input::get('LAN_KIEM_TRA'),
                'aOption'      => $Option,
                'Score'        => $Score,
                'aScoreLog'    => $aScoreLog,
            ]);
    }

    public function postAjaxUpdate() {

        $Option = Cache::get('options');

        $userScore = UserScore::hocluc(Input::get('user_id'), Input::get('DOT_KIEM_TRA'), Input::get('LAN_KIEM_TRA'))->first();

        $bFlag = false;

        if (Input::get('score')) {
            if ($userScore === null) {
                $userScore = new UserScore;
            }

            if ($userScore->score != Input::get('score')) {
                $userScore->date      = date('Y-m-d');
                $userScore->user_id   = Input::get('user_id');
                $userScore->type      = 'HOC_LUC';
                $userScore->scoretype = Input::get('DOT_KIEM_TRA');
                $userScore->score     = Input::get('score');
                $userScore->data      = Input::get('LAN_KIEM_TRA');

                $userScore->save();
                $bFlag = true;
            }
        } else {
            if ($userScore) {
                $userScore->delete();
                $bFlag = true;
            }
        }

        if ($bFlag) {
            UserScoreLog::create([
                'date'      => $userScore->date,
                'user_id'   => $userScore->user_id,
                'type'      => $userScore->type,
                'scoretype' => $userScore->scoretype,
                'score'     => $userScore->score,
                'data'      => $userScore->data,
                'note'      => Input::get('note'),
                'updater'   => Auth::user()->id,
            ]);

            // Update Total HocLuc
            Classes::HocLuc(Input::get('user_id'));
        }

        $aResponse = array (
            'status'       => 200,
            'user_id'      => Input::get('user_id'),
            'score'        => Input::get('score'),
            'DOT_KIEM_TRA' => Input::get('DOT_KIEM_TRA'),
            'LAN_KIEM_TRA' => Input::get('LAN_KIEM_TRA')
        );

        return Response::json($aResponse);
    }

}